<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
id
prev_game_id
turn
noun_id
adjective_id
noun
adjective
noun_thumbnail_url
noun_url
adjective_thumbnail_url
adjective_url
status (new, done, discarded, published)
timestamp
 */

function dt_list_games_by_status($user_id, $status) {
    $status = mysql_real_escape_string($status);
    $query = 
    "SELECT id, prev_game_id, turn, 
        noun_id, adjective_id, 
        noun, adjective, 
        noun_url, adjective_url, noun_thumbnail_url, adjective_thumbnail_url,
        gstatus, timestamp, noun_coin, adjective_coin
        FROM draw_together_games
        WHERE (noun_id = '$user_id' OR adjective_id = '$user_id')
            AND gstatus = '$status'
        ORDER BY timestamp DESC";
    //echo $query;
    return safe_select_query($query);
}

function dt_list_new_game($user_id) {
    return dt_list_games_by_status($user_id, 0);
}

function dt_list_done_game($user_id) {
    return dt_list_games_by_status($user_id, 1);
}

function dt_list_discarded_game($user_id) {
    return dt_list_games_by_status($user_id, 2);
}

function dt_list_published_game($user_id) {
    return dt_list_games_by_status($user_id, 3);
}

function dt_list_games_by_status_full($user_id, $status, $from, $to, $count, $noun, $adjective) {
    $where = "1";
    $status = mysql_real_escape_string($status);
    
    if($from > 0 && $from > $to) {
        $from = mysql_real_escape_string($from);
        $where .= " AND $id < $from ";
        if($to > 0) {
            $to = mysql_real_escape_string($to);
            $where .= " AND id > $to ";
        }
    }
    
    if ($user_id && $user_id > 0) {
        $user_id = mysql_real_escape_string($user_id);
        $where .= " AND (noun_id = '$user_id' OR adjective_id = '$user_id')";
    }
    $limit = "";
    if ($count && $count > 0) {
        $count = mysql_real_escape_string($count);
        $limit = " LIMIT 0, $count";
    }
    if($noun && strlen($noun) >= 3) {
        $noun = mysql_real_escape_string($count);
        $where .= " AND noun LIKE '%$noun%' ";
    }
    if($adjective && strlen($adjective) >= 3) {
        $adjective = mysql_real_escape_string($adjective);
        $where .= " AND adjective LIKE '%$adjective%' ";
    }
    $query = 
    "SELECT id, prev_game_id, turn, 
        noun_id, adjective_id, 
        noun, adjective, 
        noun_url, adjective_url, noun_thumbnail_url, adjective_thumbnail_url,
        gstatus, timestamp, noun_coin, adjective_coin
        FROM draw_together_games
        WHERE gstatus = '$status'
            AND $where
        ORDER BY id DESC
        $limit";
    //echo $query;
    return safe_select_query($query);
}

function dt_list_new_game_full($user_id, $from, $to, $count, $noun, $adjective) {
    return dt_list_games_by_status_full($user_id, 0, $from, $to, $count, $noun, $adjective);
}

function dt_list_done_game_full($user_id, $from, $to, $count, $noun, $adjective) {
    return dt_list_games_by_status_full($user_id, 1, $from, $to, $count, $noun, $adjective);
}

function dt_list_discarded_game_full($user_id, $from, $to, $count, $noun, $adjective) {
    return dt_list_games_by_status_full($user_id, 2, $from, $to, $count, $noun, $adjective);
}

function dt_list_published_game_full($user_id, $from, $to, $count, $noun, $adjective) {
    return dt_list_games_by_status_full($user_id, 3, $from, $to, $count, $noun, $adjective);
}

function dt_create_game($noun_id, $adjective_id, $noun, $noun_url, $noun_thumbnail_url, $prev_game_id, $turn, $noun_coin) {
    $timestamp = time();
    $noun_id = mysql_real_escape_string($noun_id);
    $adjective_id = mysql_real_escape_string($adjective_id);
    $noun = mysql_real_escape_string($noun);
    $noun_url = mysql_real_escape_string($noun_url);
    $noun_thumbnail_url = mysql_real_escape_string($noun_thumbnail_url);
    $prev_game_id = mysql_real_escape_string($prev_game_id);
    $turn = mysql_real_escape_string($turn);
    $noun_coin = mysql_real_escape_string($noun_coin);
    if (!$noun_id || $noun_id == $adjective_id || !$noun || strlen($noun) <= 0 || !$noun_coin || $noun_coin != 5) {
        return FALSE;
    }
    $query = "INSERT INTO draw_together_games(noun_id, adjective_id, noun, noun_url, noun_thumbnail_url, prev_game_id, turn, timestamp, noun_coin) 
                VALUES ('$noun_id', '$adjective_id', '$noun', '$noun_url', '$noun_thumbnail_url', '$prev_game_id', '$turn', '$timestamp', '$noun_coin')";
    return safe_insert_query($query);
}

function check_game_holder($noun_id, $adjective_id, $prev_game_id) {
    $noun_id = mysql_real_escape_string($noun_id);
    $adjective_id = mysql_real_escape_string($adjective_id);
    $prev_game_id = mysql_real_escape_string($prev_game_id);
    $query = "SELECT * FROM draw_together_games
                WHERE prev_game_id = '$prev_game_id'
                AND noun_id = '$noun_id'
                AND adjective_id = '$adjective_id'
                LIMIT 0,1";
    $result = safe_select_query($query);
    return $result && $result[0];
}

function get_game_holder_prev_game_id($noun_id, $adjective_id) {
    $noun_id = mysql_real_escape_string($noun_id);
    $adjective_id = mysql_real_escape_string($adjective_id);
    $prev_game_id = mysql_real_escape_string($prev_game_id);
    $query = "SELECT id FROM draw_together_games
                WHERE noun_id = '$noun_id'
                AND adjective_id = '$adjective_id'
                AND gstatus IN (0, 1)
                ORDER BY gstatus DESC
                LIMIT 0,1";
    $result = safe_select_query($query);
    return $result[0]->id;
}

function dt_create_game_holder($noun_id, $adjective_id, $prev_game_id, $turn) {
    $timestamp = time();
    $noun_id = mysql_real_escape_string($noun_id);
    $adjective_id = mysql_real_escape_string($adjective_id);
    $prev_game_id = mysql_real_escape_string($prev_game_id);
    $turn = mysql_real_escape_string($turn);
    if (!$noun_id || $noun_id == $adjective_id || check_game_holder($noun_id, $adjective_id, $prev_game_id)) {
        return FALSE;
    }
    if ($prev_game_id == 0) {
        $prev_game_id = get_game_holder_prev_game_id($noun_id, $adjective_id);
    }
    if ($prev_game_id == 0) {
        return FALSE;
    }
    $query = "INSERT INTO draw_together_games(noun_id, adjective_id, prev_game_id, turn, timestamp) 
                VALUES ('$noun_id', '$adjective_id', '$prev_game_id', '$turn', '$timestamp')";
    //echo $query;
    return safe_insert_query($query);
}

function dt_update_game_holder($noun_id, $adjective_id, $prev_game_id, $noun, $noun_url, $noun_thumbnail_url, $noun_coin) {
    $timestamp = time();
    $prev_game_id = mysql_real_escape_string($prev_game_id);
    $noun_id = mysql_real_escape_string($noun_id);
    $adjective_id = mysql_real_escape_string($adjective_id);
    $noun = mysql_real_escape_string($noun);
    $noun_url = mysql_real_escape_string($noun_url);
    $noun_thumbnail_url = mysql_real_escape_string($noun_thumbnail_url);
    $noun_coin = mysql_real_escape_string($noun_coin);
    if (!$noun || strlen($noun) <= 0 || !$noun_coin || $noun_coin != 5) {
        return FALSE;
    }
    if ($prev_game_id == 0) {
        $prev_game_id = get_game_holder_prev_game_id($adjective_id, $noun_id);
    }
    if ($prev_game_id == 0) {
        return FALSE;
    }
    $query = "
    UPDATE draw_together_games 
    SET noun = '$noun', 
    noun_url = '$noun_url', 
    noun_thumbnail_url = '$noun_thumbnail_url', 
    noun_coin = '$noun_coin',
    timestamp = '$timestamp' 
    WHERE prev_game_id = '$prev_game_id'
        AND noun_id = '$noun_id'
        AND adjective_id = '$adjective_id'";
    return safe_update_query($query);
}

function dt_update_game($id, $adjective, $adjective_url, $adjective_thumbnail_url, $adjective_coin) {
    $timestamp = time();
    $id = mysql_real_escape_string($id);
    $adjective = mysql_real_escape_string($adjective);
    $adjective_url = mysql_real_escape_string($adjective_url);
    $adjective_thumbnail_url = mysql_real_escape_string($adjective_thumbnail_url);
    if (!$adjective || strlen($adjective) <= 0 || $adjective_coin != 10) {
        return FALSE;
    }
    $query = "
    UPDATE draw_together_games 
    SET adjective = '$adjective', 
    adjective_url = '$adjective_url', 
    adjective_thumbnail_url = '$adjective_thumbnail_url', 
    adjective_coin = '$adjective_coin',
    gstatus = 1, timestamp = '$timestamp' 
    WHERE id = '$id'";
    return safe_update_query($query);
}

function dt_publish_game($id) {
    $timestamp = time();
    $id = mysql_real_escape_string($id);
    $query = "UPDATE draw_together_games SET gstatus = 3, timestamp = '$timestamp' WHERE id = '$id'";
    //echo $query;
    return safe_update_query($query);
}

function dt_discard_game($id) {
    $timestamp = time();
    $id = mysql_real_escape_string($id);
    $query = "UPDATE draw_together_games SET turn = 0, timestamp = '$timestamp' WHERE prev_game_id = '$id'";
    safe_update_query($query);
    $query = "UPDATE draw_together_games SET gstatus = 2, timestamp = '$timestamp' WHERE id = '$id'";
    return safe_update_query($query);
}

function dt_get_game($id) {
    $id = mysql_real_escape_string($id);
    $query = "SELECT id, prev_game_id, turn, 
        noun_id, adjective_id, 
        noun, adjective, 
        noun_url, adjective_url, noun_thumbnail_url, adjective_thumbnail_url,
        gstatus, timestamp
        FROM draw_together_games
        WHERE id = '$id'";
    $result = safe_select_query($query);
    return $result[0];
}

function dt_random_games( $user_id, $noun, $adjective, $count) {
    $user_id = mysql_real_escape_string($user_id);
    $noun = mysql_real_escape_string($noun);
    $adjective = mysql_real_escape_string($adjective);
    $count = mysql_real_escape_string($count);
    $query = "SELECT id, prev_game_id, turn, 
        noun_id, adjective_id, 
        noun, adjective, 
        noun_url, adjective_url, noun_thumbnail_url, adjective_thumbnail_url,
        gstatus, timestamp
        FROM draw_together_games
        WHERE 1
        AND noun_id <> '$user_id' AND adjective_id <> '$user_id'
        AND noun = '$noun' AND adjective = '$adjective'
        ORDER BY RAND()
        LIMIT 0, $count";
    //echo $query;
    // AND noun_id <> '$user_id' AND adjective_id <> '$user_id'
    return safe_select_query($query);
}

/*
     * id
     * user_id
     * game_id
     * timestamp
     */

function dt_rate_game($user_id, $game_id) {
    $user_id = mysql_real_escape_string($user_id);
    $game_id = mysql_real_escape_string($game_id);
    $timestamp = time();
    $query = "INSERT INTO draw_together_game_ratings(user_id, game_id, timestamp)
                VALUES('$user_id', '$game_id', '$timestamp')";
    //echo $query;
    return safe_insert_query($query);
}

?>
